package com.zjut.an.role.action;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zjut.an.role.dao.AnRole;
import com.zjut.an.role.manager.AnRoleManager;
import com.zjut.an.util.tools.JSONUtilClass;

public class AnRoleAction extends ActionSupport implements
ServletResponseAware, ServletRequestAware, ModelDriven<AnRole>{

	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	
	private AnRole obj;
	private Map<String , Object> dataMap;
	private String order;
	private String sort;
	private String search;
	private String rowstr;
	
	@Resource(name = "AnRoleManager")
	private AnRoleManager manager;
	
	public String anRoleList() throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		dataMap = new HashMap<String, Object>();
		
		dataMap.put("rows", manager.getDatas());
		dataMap.put("total", manager.getDatas().size());
		obj = null; 
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String anRoleDeletes() {
		String objumniJSON = rowstr;
		manager.deleteDatas(JSONUtilClass.getDTOList(objumniJSON, AnRole.class));
		dataMap = new HashMap<String, Object>();
		dataMap.put("flag", "success");
		return SUCCESS;
	}
	
	public String anRoleAdd() {
		System.out.print("xxxxx");
		String objumniJSON = rowstr;
		manager.addData((AnRole)JSONUtilClass.getDTO(objumniJSON, AnRole.class));
		return SUCCESS;
	}
	
	public String anRoleDelete() {
		String objumniJSON = rowstr;
		manager.deleteData((AnRole)JSONUtilClass.getDTO(objumniJSON, AnRole.class));
		dataMap = new HashMap<String, Object>();
		dataMap.put("flag", "success");
		return SUCCESS;
	}
	
	public String anRoleUpdate() {
		String objumniJSON = rowstr;
		manager.updateData((AnRole)JSONUtilClass.getDTO(objumniJSON, AnRole.class));
		return SUCCESS;
	}
	
	public String anRoleSave() {
		System.out.println("bbbbbbbbbb"+obj);
		obj = (AnRole)(JSONUtilClass.getDTOArray(rowstr, AnRole.class)[0]);
		if (obj.getRoId()!=null) {
			//-------update-------
			manager.updateData(obj);
		} else {
			//-------insert-------
			manager.addData(obj);
		}
		rowstr = null;
		return SUCCESS;
	}
	
	public String getAnRoles() throws Exception {
		request.setCharacterEncoding("UTF-8");
		dataMap = new HashMap<String, Object>();
		dataMap.put("anRoles", manager.getDatas());	
		return SUCCESS;
	}
	
	@Override
	public AnRole getModel() {
		if (null == obj) {
			obj = new AnRole();
		}
		return obj;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
		session = request.getSession();
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		response = arg0;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public AnRole getAl() {
		return obj;
	}

	public void setAl(AnRole obj) {
		this.obj = obj;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public String getRowstr() {
		return rowstr;
	}

	public void setRowstr(String rowstr) {
		this.rowstr = rowstr;
	}

	public AnRoleManager getManager() {
		return manager;
	}

	public void setManager(AnRoleManager manager) {
		this.manager = manager;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

}
